package junit.admin;

import com.jfinal.kit.PathKit;
import io.jboot.test.MockMvcResult;
import junit.base.AdminBaseTester;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class AdminAttachmentTester extends AdminBaseTester {

    @Test
    public void startAttachmentTestCases() {

        doLogin();

        testUploadAttachment();

        testDeleteAttachment();

    }


    /**
     * 测试上传附件
     */
    public void testUploadAttachment() {

        File file = new File("/Users/michael/Desktop/images/girl01.jpg");
        if (!file.exists()) {
            return;
        }

        Map<String, Object> paras = new HashMap<>();
        paras.put("file", file);
        MockMvcResult result = uploadWithCsrfToken("/commons/attachment/upload", paras);
        Long attachmentId = result.getContentAsJSONObject().getLong("attachmentId");
        Assert.assertTrue(attachmentId != null && attachmentId > 0);

        String src = result.getContentAsJSONObject().getString("src");
        File uploadFile = new File(PathKit.getWebRootPath(), src);

        Assert.assertTrue(uploadFile.exists());

        putContainerData("attachmentId", attachmentId);
    }


    /**
     * 测试删除附件
     */
    public void testDeleteAttachment() {
        Long attachmentId = (Long) getContainerData("attachmentId");
        if (attachmentId == null) {
            return;
        }

        MockMvcResult result = getWithCsrfToken("/admin/attachment/doDel/" + attachmentId);
        result.printResult();

        Assert.assertEquals(result.getStatus(), 200);
    }

}
